using System;
using NTTHAO.CommonWeb;
using System.Web.UI;

public partial class admin_webparts_Plug_ins_Images_ImagesManagement : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;
        if (action != null)
        {
            if (action.Equals("albumlist", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "AlbumList.ascx";
            }
            else if (action.Equals("albumadd", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "AlbumInput.ascx";
            }
            else if (action.Equals("albumedit", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "AlbumInput.ascx";
            }
            else if (action.Equals("viewalbum", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ImageList.ascx";
            }
            else if (action.Equals("imageadd", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ImageInput.ascx";
            }
            else if (action.Equals("imageedit", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ImageInput.ascx";
            }
            else if (action.Equals("movealbumup", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveAlbumUp(Int32.Parse(Request.QueryString["albumid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"]);
            }
            else if (action.Equals("movealbumdown", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveAlbumDown(Int32.Parse(Request.QueryString["albumid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"]);
            }
            else if (action.Equals("moveimageup", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageUp(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewalbum&albumid=" + Request.QueryString["albumid"]);
            }
            else if (action.Equals("moveimagedown", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageDown(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewalbum&albumid=" + Request.QueryString["albumid"]);
            }
            else if (action.Equals("imagedel", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.DeleteImage(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewalbum&albumid=" + Request.QueryString["albumid"]);
            }
            else
            {
                controlName = "AlbumList.ascx";
            }
        }
        else
        {
            controlName = "AlbumList.ascx";
        }

        if (controlName != null)
        {
            Control ctrl = LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
    }

    private void DeleteImage(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.ImageAlbum.AlbumDetail detail = new NTTHAO.Web.BL.ImageAlbum.AlbumDetail(imageID);
            NTTHAO.Web.BL.ImageAlbum.ImageAlbum album = new NTTHAO.Web.BL.ImageAlbum.ImageAlbum(detail.AlbumID);

            string imageFileName = "../" + album.ImagePath + detail.ImageFileName;
            if (System.IO.File.Exists(Server.MapPath(imageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(imageFileName));
            }

            detail.Delete();
        }
        catch (Exception) { }
    }

    private void MoveAlbumUp(int albumID)
    {
        try
        {
            NTTHAO.Web.BL.ImageAlbum.ImageAlbum album = new NTTHAO.Web.BL.ImageAlbum.ImageAlbum(albumID);
            album.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveAlbumDown(int albumID)
    {
        try
        {
            NTTHAO.Web.BL.ImageAlbum.ImageAlbum album = new NTTHAO.Web.BL.ImageAlbum.ImageAlbum(albumID);
            album.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveImageUp(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.ImageAlbum.AlbumDetail detail = new NTTHAO.Web.BL.ImageAlbum.AlbumDetail(imageID);
            detail.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveImageDown(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.ImageAlbum.AlbumDetail detail = new NTTHAO.Web.BL.ImageAlbum.AlbumDetail(imageID);
            detail.MoveDown();
        }
        catch (Exception) { }
    }
}
